package com.shundr.cargo.http;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.shundr.cargo.model.CargoInfo;
import com.shundr.frame.constant.LCommands;
import com.shundr.frame.constant.LContext;
import com.shundr.frame.http.HttpResult;
import com.shundr.frame.http.LHttpRequest;
import com.shundr.frame.http.LHttpRequest.RequestCompleteListener;

public class CargoRequest extends LHttpRequest implements RequestCompleteListener{
	
	public static final int GET_CARGO_LIST_SUCCESSED = 1;//获取货源列表成功
	public static final int GET_CARGO_INFO_SUCCESSED = 2;//获取货源详情成功
	
	public static final int GET_CARGO_LIST_FAILED = -1;//获取货源列表失败
	public static final int GET_CARGO_INFO_FAILED = -2;//获取货源详情失败

	
	private Handler mHandler;
	private Message mMessage;
	private Context mContext;
	
	private Map<String, Object> param;
	private String command;
	
	public CargoRequest(Context context, Handler handler){
		this.mContext = context;
		this.mHandler = handler;
		this.mMessage = mHandler.obtainMessage();
		this.setRequestCompleteListener(this);
	}
	
	/**
	 * 查询货源总数量，因为查询货源的时候已经可以返回总数量，这里只是单纯记录查询操作，可以不用处理返回
	 * getCargoLsit(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param cargoCategory 货物类型,名称
	 * @param cargoTruckType 货车类型
	 * @param cargoTruckLength 货车长度
	 * @param cargoAmount 货物数量
	 * @param srcPlaceCode 起始地址代码
	 * @param dstPlaceCode 目标 地址 代码
	 * @param startSize 记录起始位置
	 * @param size 记录条数 
	 * @exception
	 * @since  1.0.0
	 */
	public void getCargoLsit(String cargoCategory, String cargoTruckType,
			String cargoTruckLength, String cargoAmount, String srcPlaceCode,
			String dstPlaceCode, int startSize, int size){
		param = new HashMap<String, Object>();
		param.put("cargoCategory", cargoCategory);
		param.put("cargoTruckTypeCode", cargoTruckType);
		param.put("cargoTruckLength", cargoTruckLength);
		param.put("cargoAmount", cargoAmount);
		param.put("srcPlaceCode", srcPlaceCode);
		param.put("dstPlaceCode", dstPlaceCode);
		param.put("startSize", startSize);//
		param.put("size", size);
		command = LCommands.GET_CARGO_LIST;
		requestByGet(param);
	}
	
	/**
	 * 查询货源总数量，因为查询货源的时候已经可以返回总数量，这里只是单纯记录查询操作，可以不用处理返回
	 * getSearchCargoListNum(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param cargoCategory
	 * @param cargoTruckType
	 * @param cargoTruckLength
	 * @param cargoAmount
	 * @param srcPlaceCode
	 * @param dstPlaceCode
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getSearchCargoListNum(String cargoCategory, String cargoTruckType,
			String cargoTruckLength, String cargoAmount, String srcPlaceCode,
			String dstPlaceCode) {
		param = new HashMap<String, Object>();
		param.put("cargoCategory", cargoCategory);
		param.put("cargoTruckTypeCode", cargoTruckType);
		param.put("cargoTruckLength", cargoTruckLength);
		param.put("cargoAmount", cargoAmount);
		param.put("srcPlaceCode", srcPlaceCode);
		param.put("dstPlaceCode", dstPlaceCode);
		command = LCommands.GET_CARGO_LIST_NUM;
		requestByGet(param);
	}
	/**
	 * 货源详情
	 * @param cargoId
	 */
	public void getCargoInfo(String cargoId){
		param = new HashMap<String, Object>();
		String userid="";
	    if(LContext.userInfo!=null)
	    	userid=LContext.userInfo.getUserId()+"";
		param.put("userId", userid);
		param.put("cargoId", cargoId);
		command = LCommands.GET_CARGO_INFO;
		requestByGet(param);
	}
	
	/**
	 * 获取积分货源积分
	 * getCargoCredit(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param cargoId
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getCargoCredit(String cargoId){
		param = new HashMap<String, Object>();
		param.put("cargoId", cargoId);
		command = LCommands.GET_CARGO_CREDIT;
		requestByPost(param);
	}
	
	@Override
	protected void initParams() {
		super.initParams();
		mParam = param;
		mCommand = command;
		mBaseContext = mContext;
	}

	@Override
	public void requestSuccessed(HttpResult result) {
		if(mHandler == null)
			return;
		if (command.equals(LCommands.GET_CARGO_LIST)) {
			mMessage.what = GET_CARGO_LIST_SUCCESSED;
			List<CargoInfo> infos = CargoParser.parserCargoList(result.getResult());
			mMessage.obj = infos;	
		}else if (command.equals(LCommands.GET_CARGO_INFO)) {
			mMessage.what = GET_CARGO_INFO_SUCCESSED;
			CargoInfo infos = CargoParser.parserCargoInfo(result.getResult());
			mMessage.obj = infos;									
		} else if (command.equals(LCommands.GET_CARGO_LIST_NUM)) {
			return;
		}
		mHandler.sendMessage(mMessage);
	}

	@Override
	public void requestFailed(HttpResult result) {
		if(mHandler == null)
			return;
		//请求失败,result的返回码可判断失败原因，进行如下操作
		mMessage.what = REQUEST_FAILED;
		mMessage.obj = result.getDescription();
		mHandler.sendMessage(mMessage);
	}

}
